package cn.itcast.bos.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.CourierService;
import cn.itcast.bos.utils.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends BaseAction<Courier> {

	@Autowired
	private CourierService courierService;

	@Action(value = "courier_save", results = { @Result(name = "success", type = "redirect", location = "/pages/base/courier.html") })
	public String save() {
		courierService.save(model);
		return SUCCESS;
	}

	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String findAllByPage() {
		Pageable pageable = new PageRequest(page - 1, rows);
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				// courierNum,type,company,standard.name进行条件的编译
				ArrayList<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(
							root.get("courierNum").as(String.class),
							model.getCourierNum());
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p2 = cb.equal(root.get("type").as(String.class),
							model.getType());
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p3 = cb.like(
							root.get("company").as(String.class),
							"%" + model.getCompany() + "%");
					list.add(p3);
				}
				if (model.getStandard() != null
						&& StringUtils
								.isNotBlank(model.getStandard().getName())) {
					Join<Object, Object> standardRoot = root.join("standard");
					Predicate p4 = cb.like(
							standardRoot.get("name").as(String.class), "%"
									+ model.getStandard().getName() + "%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Courier> pageDate = courierService.findAllByPage(pageable,
				specification);
		pushPageDataToValueStack(pageDate);
		return SUCCESS;
	}

	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "courier_typeDel", results = { @Result(name = "success", location = "/pages/base/courier.html", type = "redirect") })
	public String typeDel() {
		String[] arr = ids.split(",");
		courierService.typeDel(arr);
		return SUCCESS;
	}

	@Action(value = "courier_typeAdd", results = { @Result(name = "success", location = "/pages/base/courier.html", type = "redirect") })
	public String typeAdd() {
		String[] arr = ids.split(",");
		courierService.typeAdd(arr);
		return SUCCESS;
	}

	@Action(value = "courier_findNoAssociation", results = { @Result(name = "success", type = "json") })
	public String findNoAssociation() {
		List<Courier> list = courierService.findNoAssociation();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	@Action(value = "courier_findnoassociation", 
			results = { @Result(name = "success", type = "json") })
	public String findnoassociation() {
		// 调用业务层，查询未关联定区的快递员 
		List<Courier> couriers = courierService.findNoAssociation();
		// 将查询快递员列表 压入值栈 
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
}
